//
//  TMButtons1.swift
//  ArchitectGenerics
//
//  Created by 姚胜龙 on 2017/6/8.
//  Copyright © 2017年 姚胜龙. All rights reserved.
//

import UIKit

//支持撤销命令
class TMButtons1 {

    //持有命令
    private var leftCommand:TMLeftCommand?
    private var rightCommand:TMRightCommand?
    private var fastDownCommand:TMFastDownCommand?
    private var transformCommand:TMTransformCommand?

    //持有接受者
    private var tetris:TetrisMeshine?
    //命令的队列保存到数组
    var commands = Array<TetrisCommandProtocal>()

    //初始化
    init(tetris:TetrisMeshine,leftCommand:TMLeftCommand,rightCommand:TMRightCommand,fastDownCommand:TMFastDownCommand,transformCommand:TMTransformCommand)
    {
        self.fastDownCommand = fastDownCommand
        self.leftCommand = leftCommand
        self.rightCommand = rightCommand
        self.transformCommand = transformCommand
        self.tetris = tetris
    }

    //创建执行命令的方法
    func toLeft() {
        self.addCommand(command: TMLeftCommand(tm: self.tetris!))
        self.leftCommand?.execute()
    }
    func toRight() {
        self.addCommand(command: TMRightCommand(tm: self.tetris!))
        self.rightCommand?.execute()
    }

    func fastDown() {
        self.addCommand(command: TMFastDownCommand(tm: self.tetris!))
        self.fastDownCommand?.execute()
    }
    func transform() {
        self.addCommand(command: TMTransformCommand(tm: self.tetris!))
        self.transformCommand?.execute()
    }

    //添加命令
    func addCommand(command:TetrisCommandProtocal) {
        self.commands.append(command)
    }

    //撤销命令(一个一个撤销)
    func undoCommand() {
        if self.commands.count > 0 {
            print("撤销了以下操作：")
            self.commands.removeLast().execute()
        }
    }

    //全部撤销
    func undoAllCommand() {
        for command in self.commands {
            command.execute()
        }
        print("撤销全部操作")
        self.commands.removeAll()
    }
}
